package com.demo.lifeinfo.common.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.demo.lifeinfo.R;
import com.demo.lifeinfo.adapter.MyNineImageAdapter;
import com.demo.lifeinfo.app.XBaseActivity;
import com.demo.lifeinfo.common.news.NewsDetailAc;
import com.demo.lifeinfo.entity.News;
import com.demo.lifeinfo.entity.User;
import com.demo.lifeinfo.model.GlideApp;
import com.demo.lifeinfo.widget.CustomPopup;
import com.flyco.roundview.RoundTextView;
import com.google.android.material.appbar.AppBarLayout;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.lxj.xpopup.XPopup;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * created by Raymond
 * on 2019-05-06
 */
public class MyLoveNews extends XBaseActivity {


    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.toolbar_right_btn)
    RoundTextView toolbarRightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    BaseQuickAdapter<News, BaseViewHolder> adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mylove;
    }

    @Override
    protected void initView() {

        textView3.setText("我的喜欢");
        adapter = new BaseQuickAdapter<News, BaseViewHolder>(R.layout.item_product) {
            @Override
            protected void convert(BaseViewHolder helper, News item) {
                NineGridImageView nineGridImageView = helper.getView(R.id.nindImgView);
                MyNineImageAdapter imageAdapter = new MyNineImageAdapter();
                nineGridImageView.setAdapter(imageAdapter);
                nineGridImageView.setImagesData(item.getImgs());
                GlideApp.with(MyLoveNews.this)
                        .load(item.getAuthor().getAvatar() == null ? R.drawable.ic_launcher_background : item.getAuthor().getAvatar().getFileUrl())
                        .centerCrop()
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .circleCrop()
                        .into((ImageView) helper.getView(R.id.circleImageView));
                helper.setText(R.id.textView, TextUtils.isEmpty(item.getAuthor().getNickeName()) ? "匿名" : item.getAuthor().getNickeName());
                helper.setText(R.id.expandable_text, item.getContent());
                helper.setVisible(R.id.btn_look, item.getContent().length() > 80 ? true : false);
                helper.addOnClickListener(R.id.btn_look);
                helper.addOnClickListener(R.id.circleImageView);
            }
        };

        View view = LayoutInflater.from(this).inflate(R.layout.view_empty, null);
        adapter.setEmptyView(view);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                News news = (News) adapter.getItem(position);
                if (view.getId() == R.id.btn_look) {
                    new XPopup.Builder(MyLoveNews.this)
                            .asCustom(new CustomPopup(MyLoveNews.this, news.getContent()))
                            .show();
                } else if (view.getId() == R.id.circleImageView) {
                    Intent intent = new Intent(MyLoveNews.this, PersonalnfoAc.class);
                    intent.putExtra("user", news.getAuthor());
                    startActivity(intent);
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                News news = (News) adapter.getItem(position);
                Intent intent = new Intent(MyLoveNews.this, NewsDetailAc.class);
                intent.putExtra("news", news);
                startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(MyLoveNews.this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        getMyLove();
    }

    private void getMyLove() {
        showProgressDialog();
        BmobQuery<News> query = new BmobQuery<>();
        query.addWhereRelatedTo("myLove", new BmobPointer(BmobUser.getCurrentUser(User.class)));
        query.findObjects(new FindListener<News>() {
            @Override
            public void done(List<News> list, BmobException e) {
                dimssProgressDialog();
                if (e == null) {
                    adapter.setNewData(list);
                } else {

                }
            }
        });
    }



    @OnClick(R.id.toolbar_back)
    public void onViewClicked() {
        onBackPressed();
    }
}
